package config

import (
	"ce-cloud-service/internal/define"
	"ce-cloud/pkg/log"
	"ce-cloud/pkg/tools"
	"gopkg.in/yaml.v2"
	"io/ioutil"
	"os"
)

var GlobalConfig *GateConfig

type GateConfig struct {
	Hall GateConfigBase `json:"hall" yaml:"hall"`
}

type GateConfigBase struct {
	Host string `json:"host" yaml:"host"`
	Port int    `json:"port" yaml:"port" `
}

func init() {
	GlobalConfig = new(GateConfig)
	GlobalConfig.readConfig()
}

func (g *GateConfig) readConfig() {
	var err error
	var path string
	if os.Getenv("ENV_DIR") != "" {
		path = "/app/internal/config/gate.yaml"
	} else {
		path = tools.PathGetRoot(define.ProjectName) + "/internal/config/gate.yaml"
	}
	yamlFile, err := ioutil.ReadFile(path)
	if err != nil {
		log.Errorf("Read gate config file error: path:{%s} err:%v", path, err)
		return
	}
	err = yaml.Unmarshal(yamlFile, GlobalConfig)
	if err != nil {
		log.Errorf("yaml Unmarshal err: %v", err)
		return
	}
	return
}
